package com.sky.aspect;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import io.lettuce.core.dynamic.annotation.Command;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Component
@Aspect
public class AutoFillAspect {

    /**
     * 切点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void point() {

    }

    /**
     * 切面
     */
    @Before("point()")
    public void autoFillJointCut(JoinPoint joinPoint) {
        //1.获取参数
        Object[] args = joinPoint.getArgs();
        if (Objects.isNull(args) || args.length == 0) {
            //没有任何参数
            return;
        }
        Object entity = args[0];

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();

        AutoFill autoFill = method.getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        try {
            //创建操作
            if (operationType == OperationType.INSERT) {
                //创建
                //反射拿到创建时间
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                //调用方法
                setCreateTime.invoke(entity,now);
                //反射拿到创建人
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                //调用方法
                setCreateUser.invoke(entity, BaseContext.getCurrentId());

                //更新
                //反射拿到更新时间
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //调用方法
                setUpdateTime.invoke(entity,now);
                //反射拿到更新人
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //调用
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            }
            //更新操作
            if (operationType == OperationType.UPDATE) {
                //反射拿到更新时间
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //调用方法
                setUpdateTime.invoke(entity,now);
                //反射拿到更新人
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //调用方法
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }

        log.info("进入到了切面");
    }
}
